﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entity.Admin;

namespace Service
{
    public class BaseService<TEntity> where TEntity : class
    {
        protected DBEntities db;

        //public BaseService()
        //{
        //    this.db = new CMPDB();
        //}

        public BaseService(DBEntities db)
        {
            this.db = db;
        }

        public virtual TEntity Find(params object[] keyValues)
        {
            return db.Set<TEntity>().Find(keyValues);
        }

        public virtual TEntity Create(TEntity entity)
        {
            db.Set<TEntity>().Add(entity);
            db.SaveChanges();
            return entity;
        }

        public virtual TEntity Save(TEntity entity)
        {
            db.Set<TEntity>().Attach(entity);
            db.Entry(entity).State = System.Data.EntityState.Modified;
            db.SaveChanges();
            return entity;
        }

        public virtual void Delete(TEntity entity)
        {
            db.Set<TEntity>().Attach(entity);
            db.Entry(entity).State = System.Data.EntityState.Deleted;
            db.SaveChanges();
        }
    }
}
